package com.ukee.birthday.modules.service.impl;

import com.ukee.birthday.common.utils.ExcelUtil;
import com.ukee.birthday.modules.entity.StaffEntity;
import com.ukee.birthday.modules.service.StaffService;
import com.ukee.birthday.modules.service.UploadExcelService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("uploadExcelService")
public class UploadExcelServiceImpl implements UploadExcelService {

    @Resource
    private StaffService staffService;

    @Override
    public String importProfession(MultipartFile file,Integer column) {

        Map<String, Object> map = new HashMap<>();
        // 获取文件的名称
        String fileName = file.getOriginalFilename();
        // 获取文件的后缀名
        String pattern = fileName.substring(fileName.lastIndexOf(".") + 1);
        List<List<String>> listContent = new ArrayList<>();
        List<String> title = new ArrayList<>();
        String message = "ok";

        try {
            if (file != null) {
                // 文件类型判断
                if (!ExcelUtil.isEXCEL(file)) {
                    message = "该文件不是excel文件";
                } else {
                    listContent = ExcelUtil.readExcelContents(file, pattern, column);
                    System.out.println(listContent);
                    // 文件内容判断
                    if (listContent.isEmpty()) {
                        message = "表格内容为空";
                    } else {
                        // 循环遍历
                        for (int i = 0; i < listContent.size(); i++) {
                            List<String> list = listContent.get(i);
                            StaffEntity staffEntity = new StaffEntity();
                            staffEntity.setUsername(list.get(0));
                            staffEntity.setIdNumber(numOfImport(list.get(1)));
                            staffEntity.setNumber(numOfImport(list.get(2)));
                            staffEntity.setTelnum(numOfImport(list.get(3)));
                            staffEntity.setBirthday(new Date(new SimpleDateFormat("yyyy-MM-dd").parse(list.get(4)).getTime()));
                            staffEntity.setAge(numOfImport(list.get(5)));
                            //性别
                            if(list.get(6).equals("男")){
                                staffEntity.setSex(1);
                            }else if(list.get(6).equals("女")){
                                staffEntity.setSex(2);
                            }else{
                                staffEntity.setSex(3);
                            }
                            //staffEntity.setSex(Integer.valueOf(numOfImport(list.get(6).toString())));
                            //默认导入的角色是员工
                            staffEntity.setRoleId(3);
                            //sha256加密
                            String salt = RandomStringUtils.randomAlphanumeric(20);
                            //默认密码
                            staffEntity.setPassword(new Sha256Hash("123456", salt).toHex());
                            staffEntity.setSalt(salt);
                            staffEntity.setCreateTime(new Date());
                            System.out.println(staffEntity);

                            staffService.save(staffEntity);
                            message = "导入成功！";
                        }
                    }
                }
            } else {
                message = "未选择文件";
            }
        } catch (Exception e) {
            e.printStackTrace();
            message = e.toString();
        }

        return message;
    }

    /**
     * 处理导入小数点: .0
     */
    public  static String  numOfImport(String value) {
        String[] str = value.split("\\.");
        if (str.length > 1) {
            String str1 = str[1];
            int m = Integer.parseInt(str1);
            if (m == 0) {
                return str[0];
            } else {
                return value;
            }
        }else{
            return value;
        }

    }
}
